﻿using GraphQL;
using Microsoft.EntityFrameworkCore;
using Movies.API.Data;
using Movies.API.GraphQL.Types;

namespace Movies.API.GraphQL.Queries
{
    public class MovieQuery : ObjectGraphType
    {

        public MovieQuery(MovieDbContext db)
        {
            // Query movie list
            Field<ListGraphType<MovieType>>("movies")
                .Resolve(a => db.Movies.ToList());

            // Query single movie 
            Field<MovieType>("movie")
                .Arguments(new QueryArguments(
                    new QueryArgument<NonNullGraphType<IntGraphType>>() { Name = "id" }))
                .ResolveAsync(async context =>
                {
                    var id = context.GetArgument<int>("id");
                    return await db.Movies.Include(a => a.Reviews)
                    .SingleAsync(a => a.Id == id);
                });




        }


    }
}
